import * as Def from "./../consts/Def";


export default class FirstPlayerMoveCtl extends Laya.Script3D {
    constructor() { super(); }

    public camera:Laya.Camera;

    public moveSpeed:number = 0.07;

    public moving:boolean = false;

    public freeze:boolean = false;

    public cameraPosDelta:Laya.Vector3 = new Laya.Vector3(0,0,0);

    protected _firstPlayerSp:Laya.Sprite3D;

    protected _isMouseDown:boolean = false;

    protected _cameraPos:Laya.Vector3 = new Laya.Vector3(0,0,0);

    protected _rotaion:Laya.Quaternion = new Laya.Quaternion();

    protected _yAxis:Laya.Vector3 = new Laya.Vector3(0, 1.0, 0);

    protected _heroDirection:Laya.Vector3 = new Laya.Vector3(0,0,0);

    protected _up = new Laya.Vector3(0,1,0);
    
    protected _first:boolean = true;

    public onAwake():void{
        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.mouseUp);
        this._firstPlayerSp = this.owner as Laya.Sprite3D;
    }

    public onDestroy():void {
        Laya.stage.off(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.mouseUp);
    }

    public mouseDown():void{
        this._isMouseDown = true;
    }

    public mouseUp():void{
        this._isMouseDown = false;
    }

    public onUpdate():void{
        var moving = false;
        var rotating = false;
        var yaw = 0.0;
        this.moving = false;
        if(this._isMouseDown){
           
            var centerX = Laya.stage.width/2;
            var centerY = Laya.stage.height/2;

            var mouseX = Laya.stage.mouseX;
            var mouseY = Laya.stage.mouseY;

            var radius = Math.sqrt((mouseX-centerX)*(mouseX-centerX) + (mouseY-centerY)*(mouseY-centerY));

            //direction
            var radian = Math.acos((mouseX - centerX)/radius);

            if(mouseY > centerY)
			{
				radian = 2*Math.PI - radian;
            }

            var delta = - Math.PI;

            Laya.Quaternion.createFromAxisAngle(this._yAxis, radian + delta, this._rotaion);
            this._rotaion.cloneTo(this._firstPlayerSp.transform.localRotation);
            this._firstPlayerSp.transform.localRotation = this._firstPlayerSp.transform.localRotation;

            //distance

            if(!this.freeze){
                this._firstPlayerSp.transform.getForward(this._heroDirection);

                Laya.Vector3.scale(this._heroDirection,this.moveSpeed, this._heroDirection);

                this._firstPlayerSp.transform.translate(this._heroDirection,false);

                this.moving = true;
            }
            
        }

         //camera follow first player  (test only)
         if(this.camera && (this.moving || this._first)){
            let fpPos = this._firstPlayerSp.transform.position;
            Laya.Vector3.add(fpPos, this.cameraPosDelta, this._cameraPos);
            this._cameraPos.cloneTo(this.camera.transform.position);
            this.camera.transform.position = this.camera.transform.position;
            this.camera.transform.lookAt(fpPos, this._up);
        }

        if(this._first){
            this._first = false;
        }
    }
}